package cn.xlbweb.sso.service.impl;

import cn.xlbweb.sso.entity.ClientDTO;
import cn.xlbweb.sso.entity.ServerDTO;
import cn.xlbweb.sso.repository.ServerRepository;
import cn.xlbweb.sso.server.LayuiTable;
import cn.xlbweb.sso.service.IServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: bobi
 * @date: 2019-03-18 00:02
 * @description:
 */
@Service
public class ServerServiceImpl implements IServerService {

    @Autowired
    private ServerRepository serverRepository;

    @Override
    public ServerDTO save(String token, String browser, String os) {
        ServerDTO server = new ServerDTO(token, browser, os);
        return serverRepository.save(server);
    }

    @Override
    public Boolean verifyToken(String token) {
        Boolean isExist = serverRepository.existsByToken(token);
        return isExist;
    }

    @Override
    public int deleteByToken(String token) {
        int count = serverRepository.deleteByToken(token);
        return count;
    }

    @Override
    public Boolean existsByToken(String token) {
        return serverRepository.existsByToken(token);
    }

    @Override
    public LayuiTable list(Integer page, Integer limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<ServerDTO> serverPage = serverRepository.findAll(pageable);
        List<ServerDTO> serverList = serverPage.getContent();
        Long count = serverPage.getTotalElements();
        return LayuiTable.success(count, serverList);
    }
}
